<template>
  <ga-modal
    v-model:visible="visible"
    title-align="start"
    :width="560"
    @close="handleCancel(formRef)"
  >
    <template #title>创建</template>
    <ga-spin style="width: 100%" :loading="loading">
      <ga-form ref="formRef" :model="formData">
        <ga-form-item
          field="ipAddress"
          label="IP地址"
          label-col-flex="70px"
          :rules="[RULE.NOT_NULL]"
          :validate-trigger="['change', 'input']"
        >
          <ga-select
            v-model="formData.ipAddress"
            style="width: 100%"
            placeholder="选择IP地址(可多选，支持搜索)"
            allow-clear
            allow-search
            multiple
            :max-tag-count="2"
          >
            <ga-option v-for="item in ipData" :key="item" :label="item" :value="item" />
          </ga-select>
        </ga-form-item>
        <ga-form-item
          field="type"
          label="类型"
          label-col-flex="70px"
          :rules="[RULE.NOT_NULL]"
          :validate-trigger="['change', 'input']"
        >
          <ga-radio-group type="button" v-model="formData.type">
            <ga-radio value="system_reserved">系统保留</ga-radio>
            <ga-radio value="unused">不能使用</ga-radio>
          </ga-radio-group>
        </ga-form-item>
        <ga-form-item field="remark" label="备注" label-col-flex="70px">
          <ga-textarea v-model="formData.remark" placeholder="请输入内容" />
        </ga-form-item>
      </ga-form>
    </ga-spin>
    <template #footer>
      <ga-button @click="visible = false">取消</ga-button>
      <ga-button type="primary" @click="handleOk">确定</ga-button>
    </template>
  </ga-modal>
</template>

<script setup>
import { ref, defineEmits, reactive } from "vue";
import { createIp, getIpUndistributedList } from "@/api/layer2subnet.ts";
import { Message } from "@garco-design/web-vue";
import { RULE } from "@/constant/validators";
import useLoading from "@/hooks/loading";
import { useRoute } from "vue-router";

const route = useRoute();
const { id } = route.params;
const { loading, setLoading } = useLoading(false);
const formRef = ref(null);
const ipData = ref([]);
const formData = reactive({
  ipAddress: [],
  type: "system_reserved",
  remark: "",
});
const visible = ref(false);
const open = async (record) => {
  visible.value = true;
  fetchUndistributedData();
  setLoading(true);
};
// 获取列表
const fetchUndistributedData = async () => {
  try {
    const { data } = await getIpUndistributedList({ limit: -1 }, id);
    ipData.value = data.items;
  } finally {
    setLoading(false);
  }
};
// 保存
const emits = defineEmits(["fetchData"]);
const handleOk = async () => {
  const res = await formRef.value?.validate();
  if (!res) {
    const obj = formData?.ipAddress?.map((item) => ({
      ipAddress: item,
      type: formData.type,
      remark: formData.remark,
    }));
    try {
      const { code } = await createIp(id, "reserved", obj);
      if (code === 20000) Message.success("操作成功");
      emits("fetchData");
    } finally {
      visible.value = false;
    }
  }
};
const handleCancel = (formEl) => {
  formEl.resetFields();
};
defineExpose({ open });
</script>

<style lang="scss" scoped></style>
